package cn.controller;

import cn.entity.Goods;
import cn.service.GoodsService;
import cn.service.KindService;
import cn.util.ServerResponse;
import cn.util.UsersLoginAnno;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    KindService kindService;
    //JSON
    @RequestMapping("list")
    @UsersLoginAnno
    public ServerResponse list(Integer page, Integer limit,String name){
        QueryWrapper<Goods> wrapper=new QueryWrapper<Goods>();
        if(!StringUtils.isNullOrEmpty(name)){
            wrapper.like("name",name);
        }
        wrapper.eq("isdel",0);
        IPage<Goods> page_list=new Page<Goods>(page,limit);
        page_list=goodsService.page(page_list,wrapper);
        long count=page_list.getTotal();
        List<Goods> list=page_list.getRecords();
        for(Goods g :list){
            if(g.getKid()!=null){
                g.setKind(kindService.getById(g.getKid()));
            }
        }
        return new ServerResponse("0","操作成功",count,list);
    }

    @RequestMapping("login")
    public ServerResponse login(String username, String password, String vcode,Integer role){
        // 获取验证码
        //String code = (String) session.getAttribute("code");
       // if (code != null && code.equalsIgnoreCase(vcode)) {
            QueryWrapper<Goods> wrapper=new QueryWrapper<Goods>();
            wrapper.eq("username",username);
            wrapper.eq("password",password);
            wrapper.eq("role",role);
            wrapper.eq("isdel",0);
            List<Goods> list=goodsService.list(wrapper);
            if(list.size()>0){
                Goods goods=list.get(0);
                return new ServerResponse("0","登陆成功!",1L,goods);
            }else{
                return new ServerResponse("1","用户名或密码错误!");
            }
//        }else{
//            return  new ServerResponse("1", "验证码错误!");
//        }
    }

    @RequestMapping("add")
    public ServerResponse add(Goods goods){
        goods.setZan(0);
        goods.setXiao(0);
        goods.setIsdel(0);
        boolean flag=goodsService.save(goods);
        if(flag){
            return new ServerResponse("0","操作成功!");
        }else{
            return new ServerResponse("1","操作失败!");
        }
    }

    @RequestMapping("update")
    public ServerResponse update(Goods goods){
        boolean flag=goodsService.updateById(goods);
        if(flag){
            return new ServerResponse("0","操作成功!");
        }else{
            return new ServerResponse("1","操作失败!");
        }
    }

    @RequestMapping("delete")
    public ServerResponse delete(Goods goods){
        goods.setIsdel(1);
        boolean flag=goodsService.updateById(goods);
        if(flag){
            return new ServerResponse("0","操作成功!");
        }else{
            return new ServerResponse("1","操作失败!");
        }
    }
}
